﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class MicroWebsiteGiftExchangeDetail
    {
        public int Add(Chain.Model.MicroWebsiteGiftExchangeDetail model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into MicroWebsiteGiftExchangeDetail(");
            builder.Append("ExchangeID,ExchangeGiftID,ExchangeNumber,ExchangePoint,GiftName)");
            builder.Append(" values (");
            builder.Append("@ExchangeID,@ExchangeGiftID,@ExchangeNumber,@ExchangePoint,@GiftName)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ExchangeID", SqlDbType.Int, 4), new SqlParameter("@ExchangeGiftID", SqlDbType.Int, 4), new SqlParameter("@ExchangeNumber", SqlDbType.Int, 4), new SqlParameter("@ExchangePoint", SqlDbType.Int, 4), new SqlParameter("@GiftName", SqlDbType.NVarChar, 200) };
            cmdParms[0].Value = model.ExchangeID;
            cmdParms[1].Value = model.ExchangeGiftID;
            cmdParms[2].Value = model.ExchangeNumber;
            cmdParms[3].Value = model.ExchangePoint;
            cmdParms[4].Value = model.GiftName;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int ExchangeDetailID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MicroWebsiteGiftExchangeDetail ");
            builder.Append(" where ExchangeDetailID=@ExchangeDetailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ExchangeDetailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ExchangeDetailID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string ExchangeDetailIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MicroWebsiteGiftExchangeDetail ");
            builder.Append(" where ExchangeDetailID in (" + ExchangeDetailIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int ExchangeDetailID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from MicroWebsiteGiftExchangeDetail");
            builder.Append(" where ExchangeDetailID=@ExchangeDetailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ExchangeDetailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ExchangeDetailID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataTable GetGiftExchangeDetailByExchangeID(int exchangeID)
        {
            return DbHelperSQL.Query("select MicroWebsiteGiftExchangeDetail.GiftName,ExchangeGiftID,ExchangeNumber, ExchangePoint from MicroWebsiteGiftExchangeDetail join PointGift on MicroWebsiteGiftExchangeDetail.ExchangeGiftID=PointGift.GiftID where ExchangeID=@ExchangeID", new SqlParameter[] { new SqlParameter("ExchangeID", exchangeID) }).Tables[0];
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ExchangeDetailID,ExchangeID,ExchangeGiftID,ExchangeNumber,ExchangePoint,MicroWebsiteGiftExchangeDetail.GiftName ");
            builder.Append(" FROM MicroWebsiteGiftExchangeDetail,PointGift ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" ExchangeDetailID,ExchangeID,ExchangeGiftID,ExchangeNumber,ExchangePoint,GiftName ");
            builder.Append(" FROM MicroWebsiteGiftExchangeDetail ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.ExchangeDetailID desc");
            }
            builder.Append(")AS Row, T.*  from MicroWebsiteGiftExchangeDetail T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public Chain.Model.MicroWebsiteGiftExchangeDetail GetModel(int ExchangeDetailID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 ExchangeDetailID,ExchangeID,ExchangeGiftID,ExchangeNumber,ExchangePoint,GiftName from MicroWebsiteGiftExchangeDetail ");
            builder.Append(" where ExchangeDetailID=@ExchangeDetailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ExchangeDetailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ExchangeDetailID;
            Chain.Model.MicroWebsiteGiftExchangeDetail detail = new Chain.Model.MicroWebsiteGiftExchangeDetail();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["ExchangeDetailID"] != null) && (set.Tables[0].Rows[0]["ExchangeDetailID"].ToString() != ""))
            {
                detail.ExchangeDetailID = int.Parse(set.Tables[0].Rows[0]["ExchangeDetailID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ExchangeID"] != null) && (set.Tables[0].Rows[0]["ExchangeID"].ToString() != ""))
            {
                detail.ExchangeID = int.Parse(set.Tables[0].Rows[0]["ExchangeID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ExchangeGiftID"] != null) && (set.Tables[0].Rows[0]["ExchangeGiftID"].ToString() != ""))
            {
                detail.ExchangeGiftID = int.Parse(set.Tables[0].Rows[0]["ExchangeGiftID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ExchangeNumber"] != null) && (set.Tables[0].Rows[0]["ExchangeNumber"].ToString() != ""))
            {
                detail.ExchangeNumber = int.Parse(set.Tables[0].Rows[0]["ExchangeNumber"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ExchangePoint"] != null) && (set.Tables[0].Rows[0]["ExchangePoint"].ToString() != ""))
            {
                detail.ExchangePoint = int.Parse(set.Tables[0].Rows[0]["ExchangePoint"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GiftName"] != null) && (set.Tables[0].Rows[0]["GiftName"].ToString() != ""))
            {
                detail.GiftName = set.Tables[0].Rows[0]["GiftName"].ToString();
            }
            return detail;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM MicroWebsiteGiftExchangeDetail ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.MicroWebsiteGiftExchangeDetail model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update MicroWebsiteGiftExchangeDetail set ");
            builder.Append("ExchangeID=@ExchangeID,");
            builder.Append("ExchangeGiftID=@ExchangeGiftID,");
            builder.Append("ExchangeNumber=@ExchangeNumber,");
            builder.Append("ExchangePoint=@ExchangePoint,");
            builder.Append("GiftName=@GiftName");
            builder.Append(" where ExchangeDetailID=@ExchangeDetailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ExchangeID", SqlDbType.Int, 4), new SqlParameter("@ExchangeGiftID", SqlDbType.Int, 4), new SqlParameter("@ExchangeNumber", SqlDbType.Int, 4), new SqlParameter("@ExchangePoint", SqlDbType.Int, 4), new SqlParameter("@GiftName", SqlDbType.NVarChar, 200), new SqlParameter("@ExchangeDetailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.ExchangeID;
            cmdParms[1].Value = model.ExchangeGiftID;
            cmdParms[2].Value = model.ExchangeNumber;
            cmdParms[3].Value = model.ExchangePoint;
            cmdParms[4].Value = model.GiftName;
            cmdParms[5].Value = model.ExchangeDetailID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

